//
//  ClassifyGongLvViewController.m
//  礼物说
//
//  Created by dllo on 16/4/21.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "ClassifyGongLvViewController.h"

/**专题 */
#define kCLASSITYZHUANTIURL [NSString stringWithFormat: @"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=2&limit=20&offset=%ld",num, page]

/**攻略 */
#define kCLASSITYGONGLVURL  [NSString stringWithFormat: @"http://api.liwushuo.com/v2/channels/%@/items?limit=20&offset=%ld",num, page]

@interface ClassifyGongLvViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;
/**专题的数组 */
@property(nonatomic, retain) NSMutableArray * dataArr;
/**攻略的数组 */
@property(nonatomic, retain) NSMutableArray * bigDataArr;
/**<页面id */
@property (nonatomic, retain) NSNumber * decide;
/**<page */
@property (nonatomic, assign) NSInteger n;

@end

@implementation ClassifyGongLvViewController


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)dealloc{
    
    [_decide release];
    [_bigDataArr release];
    [_dataArr release];
    [_collectionView release];
    [_titles release];
    [_Judge release];
    [_num release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    
    [GiFHUD show];
    /**数据处理要用的数据 */
    
    self.tabBarController.tabBar.hidden = YES;
    
    self.decide = 0;
    self.dataArr = [NSMutableArray array];
    self.bigDataArr = [NSMutableArray array];
    
    self.navigationItem.title = _titles;
    
#pragma mark - 创建BarButton【返回】
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
    [leftButton release];
    
    
//    if ([_Judge isEqualToString:@"zhuanTi"]) {
//        
//        UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
//        self.navigationItem.rightBarButtonItem = rightButton;
//        [rightButton release];
//        [self dataHandle:_num Page:0];
//    }
//    if ([_Judge isEqualToString:@"gongLv"]) {
//        
//        UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonGongLvAction)];
//        
//        self.navigationItem.rightBarButtonItem = rightButton;
//        [rightButton release];
//        [self dataHandles: _num Page:0];
//    }
    
    [self createCollection];
    
}
- (void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark - 数据解析
#pragma mark - 专题
- (void)dataHandle:(NSNumber *)num Page:(NSInteger)page{
    
    [XRAFNetworkTool GETUrl:kCLASSITYZHUANTIURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray * data = responseObject[@"data"][@"posts"];
        NSMutableArray * mArr = [NSMutableArray array];
        mArr = [ZhuanTiContentModel modelHandleWithArray:data];
        if (_decide == num) {
            
            if (page == 0) {
                
                [_dataArr removeAllObjects];
            }
            
            [_dataArr addObjectsFromArray:mArr];
        }else{
            
            
            [_dataArr addObjectsFromArray:mArr];
            _decide = num;
        }
        
        if (_dataArr.count > 0) {
            
            [_collectionView reloadData];
        }
        [GiFHUD dismiss];
        
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
#pragma mark - 攻略
- (void)dataHandles:(NSNumber *)num Page:(NSInteger)page{
    
    [XRAFNetworkTool GETUrl:kCLASSITYGONGLVURL body:nil headFile:nil response:XRJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSArray * data = responseObject[@"data"][@"items"];
        NSMutableArray * array = [NSMutableArray array];
        array = [GongLvContentModel modelHandleWithArray:data];
        if (_decide == num) {
            
            if (page == 0) {
                
                [_bigDataArr removeAllObjects];
            }
            [_bigDataArr addObjectsFromArray:array];
        }else{
            
            self.bigDataArr = array;
            _decide = num;
        }
        
        if (_bigDataArr.count > 0) {
            
            [_collectionView reloadData];
        }
        [GiFHUD dismiss];
        
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}

#pragma mark - 创建Collection
- (void)createCollection{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(kWIDTH - kWIDTHS(5), kWIDTHS(150));
    layout.sectionInset = UIEdgeInsetsMake(kWIDTHS(5), kWIDTHS(5), kWIDTHS(5), kWIDTHS(5));
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, kHEIGHT - 64) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    _collectionView.bounces = YES;/**<取消回弹 */
    _collectionView.pagingEnabled = NO;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    
    [_collectionView registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self createMjbfresh];
}

#pragma mark - 添加刷新
/**上拉加载，下拉刷新 */
- (void)createMjbfresh{
    
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if ([_Judge isEqualToString:@"zhuanTi"]) {
            
            [self dataHandle:_num Page:0];
            
        }else{
            
            [self dataHandles:_num Page:0];
        }
    }];
    self.n = 0;
    _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        
        _n = _n + 20;
        
        if ([_Judge isEqualToString:@"zhuanTi"]) {
            
            [self dataHandle:_num Page: _n];
            
        }else{
            
            [self dataHandles:_num Page: _n];
        }
        
    }];
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if ([_Judge isEqualToString:@"zhuanTi"]) {
        
        return _dataArr.count;
    }else{
        
        return _bigDataArr.count;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ContentCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if ([_Judge isEqualToString:@"zhuanTi"]) {
        
        cell.model = _dataArr[indexPath.row];
    }else{
        
        cell.gongLvContentModel = _bigDataArr[indexPath.row];
    }
    return cell;
}

#pragma mark - collectionView点击方法 跳转WebView
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GongLvXiangQingViewController * gongLvVc = [[GongLvXiangQingViewController alloc] init];
    if ([_Judge isEqualToString:@"zhuanTi"]) {
        
        ZhuanTiContentModel * model = _dataArr[indexPath.row];
        gongLvVc.number = model.number;

    }
    if ([_Judge isEqualToString:@"gongLv"]) {
        
        GongLvContentModel * model = _bigDataArr[indexPath.row];
        gongLvVc.number = model.number;

    }
    
    [self.navigationController pushViewController: gongLvVc animated:YES];
    [gongLvVc release];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
